package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.RightsMapper;
import com.jt.pojo.Rights;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author SWJ
 * @create 2021-08-05 14:32
 */
@Service
public class RightServiceImpl implements RightsService {
    @Autowired
    private RightsMapper rightsMapper;

    @Override
    public List<Rights> getAll() {
        //查询全部不需要条件
        return rightsMapper.selectList(null);
        //查询rightsMapper中的查询语句
    }


    /**
     * 查询二级菜单信息
     * 1.查询一级菜单 parent_id=0
     * 2.查询二级           id=1
     */
    @Override
    public List<Rights> getRightsList() {
        //1.查询所有的一级菜单<Rights>
        //查询包装器QueryWrapper映射
        QueryWrapper queryWrapper = new QueryWrapper<>();
        //查询eq等于"parent_id",0 一级菜单
        queryWrapper.eq("parent_id", 0);
        //rightsMapper查询语句
        List<Rights> oneList = rightsMapper.selectList(queryWrapper);


        /**高效for循环
         * 第一个是类型 Rights
         * 第二个是你定义的名字 oneRights
         * 第三个是集合名字 oneList
         * */
        //先拿到一级菜单查询自己的二级菜单
        for (Rights oneRights : oneList) {
            //清空原来有条件
            queryWrapper.clear();
            //根据一级菜单的id查
            // eq等于parent_id二级oneRights查询的是Id的查询功能
            queryWrapper.eq("parent_id", oneRights.getId());
            //封装进一级菜单
            List<Rights> twoList = rightsMapper.selectList(queryWrapper);
            //实现父子关系进行封装
            //    private List<Rights> children;
            oneRights.setChildren(twoList);
        }

        //在执行完2级查询后返回1级中
        return oneList;
    }


}
